package org.assistant.csp.utils;

import java.util.ArrayList;
import java.util.List;

public class Tree<T> {
	// 顶级父结点列表
	private List<TreeNode<T>> parentTreeNodeList = new ArrayList<TreeNode<T>>();

	/**
	 * 添加一个顶级结点
	 * 
	 * @param treeNode
	 * @return
	 */
	public boolean addParentTreeNode(TreeNode<T> treeNode) {
		return parentTreeNodeList.add(treeNode);
	}

	/**
	 * 删除一个顶级结点
	 * 
	 * @param index
	 * @return
	 */
	public TreeNode<T> removeParentTreeNode(int index) {
		return parentTreeNodeList.remove(index);
	}

	/**
	 * 根据索引获取一个顶级结点
	 * 
	 * @param index
	 *            -(>=0)
	 * @return
	 */
	public TreeNode<T> getParentTreeNodeByIndex(int index) {
		return parentTreeNodeList.get(index);
	}

	/**
	 * 根据父结点ID获取顶级树结点
	 * 
	 * @param parentId
	 * @return
	 */
	public TreeNode<T> getParentTreeNodeByParent(long parentId) {
		for (int i = 0; i < parentTreeNodeList.size(); i++) {
			TreeNode<T> tmp = parentTreeNodeList.get(i);
			TreeNode<T> top = tmp.getChildById(parentId);
			if (top != null) {
				return top;
			}
		}
		return null;
	}

	/**
	 * 此树中顶级结点的总数
	 * 
	 * @return
	 */
	public int parentTreeNodeSize() {
		return parentTreeNodeList.size();
	}
}
